package com.itheima.day24;

import java.util.*;

public class MapDemo05 {
    public static void main(String[] args) {

        //练习：学生投票去景点

        String[] arr={"A","B","C","D"};
        //定义一个集合存储80位学生的投票结果
        ArrayList<String> objects = new ArrayList<>();
        Random r=new Random();
        for (int i=0;i<80;i++)
        {
            int index=r.nextInt(arr.length);
           objects.add(arr[index]);
        }
       // System.out.println(objects);

        HashMap<String, Integer> hm = new HashMap<>();

        for (String name:objects)
        {
            if(hm.containsKey(name))
            {
                int count=hm.get(name);
                count++;
                hm.put(name,count);
            }
            else
            {
                hm.put(name,1);
            }
        }

        System.out.println(hm);//{A=18, B=24, C=25, D=13}

        //取得景点的最大值
        int max=0;
        Set<Map.Entry<String, Integer>> entries = hm.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            Integer value = entry.getValue();
            if(value>max)
            {
                max=value;
            }
        }
        System.out.println(max);//23
        //判断哪个景点的次数和最大值一样

        for (Map.Entry<String, Integer> entry : entries) {
            Integer value = entry.getValue();
            if(value==max)
                System.out.println(entry.getKey());
        }
    }


}
